import {
  extend,
  padZero,
  makeArrayProp,
  clamp
} from "../utils/index.mjs";
import { pickerSharedProps } from "../picker/Picker.mjs";

// 向外暴露props, 合并pickerShareProps中的props
const sharedProps = extend({}, pickerSharedProps, {
  modelValue: makeArrayProp(),
  filter: Function, // 过滤数据方法
  formatter: { // 格式化展示内容
    type: Function,
    default: (type, option) => option
  }
});

const pickerInheritKeys = Object.keys(pickerSharedProps);

// 各个类型 生成各列需要展示的数据
function times(n, iteratee) {
  if (n < 0) {
    return [];
  }
  const result = Array(n);
  let index = -1;
  while (++index < n) {
    result[index] = iteratee(index);
  }
  return result;
}

// 获取当月最大值 设置为32
const getMonthEndDay = (year, month) => 32 - new Date(year, month - 1, 32).getDate();

// 获取各列的展示数据 max最大值 min最小值 type列的类型  formatter展示格式化 filter过滤方法, values
const genOptions = (min, max, type, formatter, filter, values) => {
  const options = times(max - min + 1, (index) => {
    // 数据前面补零
    const value = padZero(min + index);
    // 数据格式化
    return formatter(type, {
      text: value,
      value
    });
  });
  // 数据过滤
  return filter ? filter(type, options, values) : options;
};

// values绑定值  columns数据源
const formatValueRange = (values, columns) => values.map((value, index) => {
  const column = columns[index];
  if (column.length) {
    const minValue = +column[0].value;
    const maxValue = +column[column.length - 1].value;
    return padZero(clamp(+value, minValue, maxValue));
  }
  return value;
});

export {
  formatValueRange,
  genOptions,
  getMonthEndDay,
  pickerInheritKeys,
  sharedProps,
  times
};
